package theOrbalEra.client.handler;

import java.util.Iterator;
import java.util.List;

import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityLiving;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.util.AxisAlignedBB;
import net.minecraft.util.Vec3;
import net.minecraft.world.World;
import theOrbalEra.client.OrbalArts.entity.OrbalArtEntityNapalmBreath;
import theOrbalEra.client.OrbalArts.entity.OrbalArtEntityFlameArrow;
import theOrbalEra.client.OrbalArts.entity.OrbalArtEntityFireBolt;
import theOrbalEra.common.util.OrbalArtsIdList;
import theOrbalEra.common.util.OrbalUtils;
import cpw.mods.fml.client.FMLClientHandler;

/**
 * OrbalArtsHandlerClient.java
 * 
 * @author hendyzone hendyzone@163.com
 * @version 1.0
 */
public class OrbalArtsHandlerClient {
	public void doOrbalArts(int orbalArtsid, EntityPlayer player, World world) {
		switch (orbalArtsid) {
			case OrbalArtsIdList.FireBolt:
				doFireBolt(player, world);
				break;
			case OrbalArtsIdList.FlameArrow:
				doFlameArrow(player, world);
				break;
			case OrbalArtsIdList.NapalmBreath:
				doNapalmBreath(player, world);
				break;
			case OrbalArtsIdList.FireBoltEX:
				doFireBoltEX(player, world);
				break;
			case OrbalArtsIdList.SpiralFlare:
				doSpiralFlare(player, world);
				break;
			case OrbalArtsIdList.VolcanicRave:
				doVolcanicRave(player, world);
				break;
			case OrbalArtsIdList.Forte:
				doForte(player, world);
				break;
			case OrbalArtsIdList.AquaBleed:
				doAquaBleed(player, world);
				break;
			case OrbalArtsIdList.BlueImpact:
				doBlueImpact(player, world);
				break;
			case OrbalArtsIdList.DiamondDust:
				doDiamondDust(player, world);
				break;
			case OrbalArtsIdList.Tear:
				doTear(player, world);
				break;
			case OrbalArtsIdList.Teara:
				doTeara(player, world);
				break;
			case OrbalArtsIdList.Tearal:
				doTearal(player, world);
				break;
			case OrbalArtsIdList.LaTear:
				doLaTear(player, world);
				break;
			case OrbalArtsIdList.LaTeara:
				doLaTeara(player, world);
				break;
			case OrbalArtsIdList.Curia:
				doCuria(player, world);
				break;
			case OrbalArtsIdList.LaCuria:
				doLaCuria(player, world);
				break;
			case OrbalArtsIdList.Revive:
				doRevive(player, world);
				break;
			case OrbalArtsIdList.StoneHammer:
				doStoneHammer(player, world);
				break;
			case OrbalArtsIdList.EarthLance:
				doEarthLance(player, world);
				break;
			case OrbalArtsIdList.PetrifyBreath:
				doPetrifyBreath(player, world);
				break;
			case OrbalArtsIdList.StoneImpact:
				doStoneImpact(player, world);
				break;
			case OrbalArtsIdList.TitanicRoar:
				doTitanicRoar(player, world);
				break;
			case OrbalArtsIdList.EarthGuard:
				doEarthGuard(player, world);
				break;
			case OrbalArtsIdList.EarthWall:
				doEarthWall(player, world);
				break;
			case OrbalArtsIdList.Crest:
				doCrest(player, world);
				break;
			case OrbalArtsIdList.AirStrike:
				doAirStrike(player, world);
				break;
			case OrbalArtsIdList.Aerial:
				doAerial(player, world);
				break;
			case OrbalArtsIdList.AeroStorm:
				doAeroStorm(player, world);
				break;
			case OrbalArtsIdList.Lightning:
				doLightning(player, world);
				break;
			case OrbalArtsIdList.PlasmaWave:
				doPlasmaWave(player, world);
				break;
			case OrbalArtsIdList.SylphenGuard:
				doSylphenGuard(player, world);
				break;
			case OrbalArtsIdList.SylphenWing:
				doSylphenWing(player, world);
				break;
			case OrbalArtsIdList.SoulBlur:
				doSoulBlur(player, world);
				break;
			case OrbalArtsIdList.ShadowSpear:
				doShadowSpear(player, world);
				break;
			case OrbalArtsIdList.HellGate:
				doHellGate(player, world);
				break;
			case OrbalArtsIdList.WhiteGehenna:
				doWhiteGehenna(player, world);
				break;
			case OrbalArtsIdList.ClockUp:
				doClockUp(player, world);
				break;
			case OrbalArtsIdList.ClockUpEX:
				doClockUpEX(player, world);
				break;
			case OrbalArtsIdList.AntiSept:
				doAntiSept(player, world);
				break;
			case OrbalArtsIdList.AntiSeptAll:
				doAntiSeptAll(player, world);
				break;
			case OrbalArtsIdList.ChaosBrand:
				doChaosBrand(player, world);
				break;
			case OrbalArtsIdList.Saint:
				doSaint(player, world);
				break;
		}
	}

	private void doFireBoltEX(EntityPlayer player, World world) {
		Entity target = null;
		double distance = -1.0;
		double entityDis = -1.0;
		
		
		Vec3 vec3=player.getLookVec();
		@SuppressWarnings("unchecked")
		List<Entity> list = OrbalUtils.findTarget(world,AxisAlignedBB.getBoundingBox(
				player.posX - 1,player.posY - 20, player.posZ - 1, 
				player.posX + OrbalUtils.isPositive(vec3.xCoord)*20,player.posY + 20, player.posZ + OrbalUtils.isPositive(vec3.zCoord)*20),
				player);

		if(list.isEmpty()) {
			player.addChatMessage("找不到可以攻击的目标");
			return;
		}
		Iterator<Entity> iterator = list.iterator();
		
		while (iterator.hasNext()) {
			
			Entity entity = iterator.next();
//			player.addChatMessage(entity.getEntityName());
			entityDis = entity.getDistanceSqToEntity(player);
			if(distance == -1.0) {
				distance = entityDis;
				target = entity;
			}
			else {
				if(entityDis <= distance) {
					target = entity;
				}
			}
		}
	
			double x=target.posX-player.posX;
			double y=target.posY-player.posY;
			double z=target.posZ-player.posZ;
		OrbalArtEntityFireBolt fireball = new OrbalArtEntityFireBolt(world,player,(EntityLiving) target,x,y,z);
		OrbalArtEntityFireBolt fireball2 = new OrbalArtEntityFireBolt(world,player,(EntityLiving) target,x,y,z);
		OrbalArtEntityFireBolt fireball3 = new OrbalArtEntityFireBolt(world,player,(EntityLiving) target,x,y,z);
		fireball2.setPosition(player.posX+vec3.xCoord*5+2, player.posY+vec3.yCoord*5, player.posZ+vec3.zCoord*5+2);
		fireball3.setPosition(player.posX+vec3.xCoord*5-2, player.posY+vec3.yCoord*5, player.posZ+vec3.zCoord*5-2);
		if(FMLClientHandler.instance().getClient().theWorld.spawnEntityInWorld(fireball)&&
				FMLClientHandler.instance().getClient().theWorld.spawnEntityInWorld(fireball2)&&
				FMLClientHandler.instance().getClient().theWorld.spawnEntityInWorld(fireball3))
		System.out.println("fireballex shoot");
		

	}

	private void doSaint(EntityPlayer player, World world) {
		// TODO Auto-generated method stub

	}

	private void doChaosBrand(EntityPlayer player, World world) {
		// TODO Auto-generated method stub

	}

	private void doAntiSeptAll(EntityPlayer player, World world) {
		// TODO Auto-generated method stub

	}

	private void doAntiSept(EntityPlayer player, World world) {
		// TODO Auto-generated method stub

	}

	private void doClockUpEX(EntityPlayer player, World world) {
		// TODO Auto-generated method stub

	}

	private void doClockUp(EntityPlayer player, World world) {
		// TODO Auto-generated method stub

	}

	private void doWhiteGehenna(EntityPlayer player, World world) {
		// TODO Auto-generated method stub

	}

	private void doHellGate(EntityPlayer player, World world) {
		// TODO Auto-generated method stub

	}

	private void doShadowSpear(EntityPlayer player, World world) {
		// FIXME

	}

	private void doSoulBlur(EntityPlayer player, World world) {
		// TODO Auto-generated method stub

	}

	private void doSylphenWing(EntityPlayer player, World world) {
		// TODO Auto-generated method stub

	}

	private void doSylphenGuard(EntityPlayer player, World world) {
		// TODO Auto-generated method stub

	}

	private void doPlasmaWave(EntityPlayer player, World world) {
		// TODO Auto-generated method stub

	}

	private void doLightning(EntityPlayer player, World world) {
		// TODO Auto-generated method stub

	}

	private void doAeroStorm(EntityPlayer player, World world) {
		// TODO Auto-generated method stub

	}

	private void doAerial(EntityPlayer player, World world) {
		// TODO Auto-generated method stub

	}

	private void doAirStrike(EntityPlayer player, World world) {
		// TODO Auto-generated method stub

	}

	private void doCrest(EntityPlayer player, World world) {
		// TODO Auto-generated method stub

	}

	private void doEarthWall(EntityPlayer player, World world) {
		// TODO Auto-generated method stub

	}

	private void doEarthGuard(EntityPlayer player, World world) {
		// TODO Auto-generated method stub

	}

	private void doTitanicRoar(EntityPlayer player, World world) {
		// TODO Auto-generated method stub

	}

	private void doStoneImpact(EntityPlayer player, World world) {
		// TODO Auto-generated method stub

	}

	private void doPetrifyBreath(EntityPlayer player, World world) {
		// TODO Auto-generated method stub

	}

	private void doEarthLance(EntityPlayer player, World world) {
		// TODO Auto-generated method stub

	}

	private void doStoneHammer(EntityPlayer player, World world) {
		// TODO Auto-generated method stub

	}

	private void doRevive(EntityPlayer player, World world) {
		// TODO Auto-generated method stub

	}

	private void doLaCuria(EntityPlayer player, World world) {
		// TODO Auto-generated method stub

	}

	private void doCuria(EntityPlayer player, World world) {
		// TODO Auto-generated method stub

	}

	private void doLaTeara(EntityPlayer player, World world) {
		// TODO Auto-generated method stub

	}

	private void doLaTear(EntityPlayer player, World world) {
		// TODO Auto-generated method stub

	}

	private void doTearal(EntityPlayer player, World world) {
		// TODO Auto-generated method stub

	}

	private void doTeara(EntityPlayer player, World world) {
		// TODO Auto-generated method stub

	}

	private void doTear(EntityPlayer player, World world) {
		// TODO Auto-generated method stub

	}

	private void doDiamondDust(EntityPlayer player, World world) {
		// TODO Auto-generated method stub

	}

	private void doBlueImpact(EntityPlayer player, World world) {
		// TODO Auto-generated method stub

	}

	private void doAquaBleed(EntityPlayer player, World world) {
		// TODO Auto-generated method stub

	}

	private void doForte(EntityPlayer player, World world) {
		// TODO Auto-generated method stub

	}

	private void doVolcanicRave(EntityPlayer player, World world) {
		// TODO Auto-generated method stub

	}

	private void doSpiralFlare(EntityPlayer player, World world) {

		
		Entity target = null;
		double distance = -1.0;
		double entityDis = -1.0;
		Vec3 vec3=player.getLookVec();
		@SuppressWarnings("unchecked")
		List<Entity> list = OrbalUtils.findTarget(world,AxisAlignedBB.getBoundingBox(
				player.posX - 1,player.posY - 20, player.posZ - 1, 
				player.posX + OrbalUtils.isPositive(vec3.xCoord)*20,player.posY + 20, player.posZ + OrbalUtils.isPositive(vec3.zCoord)*20),
				player);
		if(list.isEmpty()) {
			player.addChatMessage("找不到可以攻击的目标");
			return;
		}
		Iterator<Entity> iterator = list.iterator();
		
		while (iterator.hasNext()) {
			
			Entity entity = iterator.next();
			if(!(entity instanceof EntityLiving))
				continue;
			
			entityDis = entity.getDistanceSqToEntity(player);
			if(distance == -1.0) {
				distance = entityDis;
				target = entity;
			}
			else {
				if(entityDis <= distance) {
					target = entity;
				}
			}
		}
		if(target instanceof EntityLiving){
			OrbalArtEntityFlameArrow flameArrow = new OrbalArtEntityFlameArrow(world,player,(EntityLiving) target,-1);
			OrbalArtEntityFlameArrow flameArrow1 = new OrbalArtEntityFlameArrow(world,player,(EntityLiving) target,0);
			OrbalArtEntityFlameArrow flameArrow2 = new OrbalArtEntityFlameArrow(world,player,(EntityLiving) target,1);
			OrbalArtEntityFlameArrow flameArrow3 = new OrbalArtEntityFlameArrow(world,player,(EntityLiving) target,2);
			OrbalArtEntityFlameArrow flameArrow4 = new OrbalArtEntityFlameArrow(world,player,(EntityLiving) target,3);
		FMLClientHandler.instance().getClient().theWorld.spawnEntityInWorld(flameArrow);
		FMLClientHandler.instance().getClient().theWorld.spawnEntityInWorld(flameArrow1);
		FMLClientHandler.instance().getClient().theWorld.spawnEntityInWorld(flameArrow2);
		FMLClientHandler.instance().getClient().theWorld.spawnEntityInWorld(flameArrow3);
		FMLClientHandler.instance().getClient().theWorld.spawnEntityInWorld(flameArrow4);
		System.out.println("Burning arrow shoot");
		}
		
		

	}

	private void doNapalmBreath(EntityPlayer player, World world) {
		Entity target = null;
		double distance = -1.0;
		double entityDis = -1.0;
		Vec3 vec3=player.getLookVec();
		@SuppressWarnings("unchecked")
		List<Entity> list = OrbalUtils.findTarget(world,AxisAlignedBB.getBoundingBox(
				player.posX - 1,player.posY - 20, player.posZ - 1, 
				player.posX + OrbalUtils.isPositive(vec3.xCoord)*20,player.posY + 20, player.posZ + OrbalUtils.isPositive(vec3.zCoord)*20),
				player);
		if(list.isEmpty()) {
			player.addChatMessage("找不到可以攻击的目标");
			return;
		}
		Iterator<Entity> iterator = list.iterator();
		
		while (iterator.hasNext()) {
			
			Entity entity = iterator.next();
			if(!(entity instanceof EntityLiving))
				continue;
			
			entityDis = entity.getDistanceSqToEntity(player);
			if(distance == -1.0) {
				distance = entityDis;
				target = entity;
			}
			else {
				if(entityDis <= distance) {
					target = entity;
				}
			}
		}
		if(target instanceof EntityLiving){
		OrbalArtEntityNapalmBreath burningAir = new OrbalArtEntityNapalmBreath(world,player,(EntityLiving) target);
	
		if(FMLClientHandler.instance().getClient().theWorld.spawnEntityInWorld(burningAir))
		System.out.println("Burning Air");
		}

	}

	private void doFlameArrow(EntityPlayer player, World world) {
		Entity target = null;
		double distance = -1.0;
		double entityDis = -1.0;
		Vec3 vec3=player.getLookVec();
		@SuppressWarnings("unchecked")
		List<Entity> list = OrbalUtils.findTarget(world,AxisAlignedBB.getBoundingBox(
				player.posX - 1,player.posY - 20, player.posZ - 1, 
				player.posX + OrbalUtils.isPositive(vec3.xCoord)*20,player.posY + 20, player.posZ + OrbalUtils.isPositive(vec3.zCoord)*20),
				player);
		if(list.isEmpty()) {
			player.addChatMessage("找不到可以攻击的目标");
			return;
		}
		Iterator<Entity> iterator = list.iterator();
		
		while (iterator.hasNext()) {
			
			Entity entity = iterator.next();
			if(!(entity instanceof EntityLiving))
				continue;
			
			entityDis = entity.getDistanceSqToEntity(player);
			if(distance == -1.0) {
				distance = entityDis;
				target = entity;
			}
			else {
				if(entityDis <= distance) {
					target = entity;
				}
			}
		}
		if(target instanceof EntityLiving){
			OrbalArtEntityFlameArrow burningArrow = new OrbalArtEntityFlameArrow(world,player,(EntityLiving) target,-1);
		FMLClientHandler.instance().getClient().theWorld.spawnEntityInWorld(burningArrow);
		System.out.println("Burning arrow shoot");
		}

	}

	private void doFireBolt(EntityPlayer player, World world) {
		//DOING 
		//BUG
		test(player,world);
//		Entity target = null;
//		double distance = -1.0;
//		double entityDis = -1.0;
////		
//		
//		Vec3 vec3=player.getLookVec();
//		@SuppressWarnings("unchecked")
//		List<Entity> list = OrbalUtils.findTarget(world,AxisAlignedBB.getBoundingBox(
//				player.posX - 1,player.posY - 20, player.posZ - 1, 
//				player.posX + OrbalUtils.isPositive(vec3.xCoord)*20,player.posY + 20, player.posZ + OrbalUtils.isPositive(vec3.zCoord)*20),
//				player);
//
//		if(list.isEmpty()) {
//			player.addChatMessage("找不到可以攻击的目标");
//			return;
//		}
//		Iterator<Entity> iterator = list.iterator();
//		
//		while (iterator.hasNext()) {
//			
//			Entity entity = iterator.next();
//			player.addChatMessage(entity.getEntityName());
//			entityDis = entity.getDistanceSqToEntity(player);
//			if(distance == -1.0) {
//				distance = entityDis;
//				target = entity;
//			}
//			else {
//				if(entityDis <= distance) {
//					target = entity;
//				}
//			}
//		}
//	
//			double x=target.posX-player.posX;
//			double y=target.posY-player.posY;
//			double z=target.posZ-player.posZ;
//		OrbalArtEntityFireBolt fireball = new OrbalArtEntityFireBolt(world,player,(EntityLiving) target,x,y,z);
//	
//		if(FMLClientHandler.instance().getClient().theWorld.spawnEntityInWorld(fireball))
//		System.out.println("fireball shoot");
		

	}

	private void test(EntityPlayer player, World world) {
		
		Vec3 vec3=player.getLookVec();
		@SuppressWarnings("unchecked")
		List<Entity> list = OrbalUtils.findTarget(world,AxisAlignedBB.getBoundingBox(
				player.posX - 10,
				player.posY - 1, 
				player.posZ - 1, 
				player.posX + OrbalUtils.isPositive(vec3.xCoord)*20,
				player.posY + OrbalUtils.isPositive(vec3.yCoord)*20,
				player.posZ + OrbalUtils.isPositive(vec3.zCoord)*20),
				player);
		world.setBlock((int)player.posX + OrbalUtils.isPositive(vec3.xCoord)*20,
				(int)player.posY + OrbalUtils.isPositive(vec3.yCoord)*20,
				(int)player.posZ + OrbalUtils.isPositive(vec3.zCoord)*20,
				3, 0,2);

		if(list.isEmpty()) {
			player.addChatMessage("找不到可以攻击的目标");
			return;
		}
		Iterator<Entity> iterator = list.iterator();
		
		while (iterator.hasNext()) {
			Entity entity = iterator.next();
			if(!world.isRemote)
			player.addChatMessage(entity.getEntityName());
			
	}
	}
}
